
package cn.huihuo.weibo.service;

import cn.huihuo.weibo.module.Comment;
import cn.huihuo.weibo.module.ProdComment;
import cn.huihuo.weibo.module.UgcHot;
import cn.huihuo.weibo.module.UserChooseChannel;

import java.util.List;

/**
 * 生成ugc的热度
 * Created by zhengjunbo on 2015/11/9.
 */
public class UgcHotService {
    public static final UgcHotService servcie = new UgcHotService();


    public void processUgcHot(){
        List<ProdComment> prodComments = ProdComment.prodComment.find("select * from prodcomment ");
        for(ProdComment prodComment:prodComments){
            statisticalUgcHot(String.valueOf(prodComment.getInt("id")));
        }
    }

    /**
     * 摘抄或者评论分数都是+1，
     * 购买渠道，需要检查该评论是否对渠道投过票，如果投过则，好评分+2，评论分+1.5，如果中评，投票分1，评论1，差评，投票分-2，评论分-0.5
     * @param ugcId
     */
    public void statisticalUgcHot(String ugcId){
        //首先需要知道该条ugc的类型
        ProdComment prodComment = ProdComment.prodComment.findById(ugcId);
        if(prodComment==null){
            return;
        }



        String ugcType = prodComment.getStr("ugc_type");

        if("1".equals(ugcType)){
            return;
        }
        double weight = 0;

        if("4".equals(ugcType)){ //渠道
            List<UserChooseChannel> chooseChannels = UserChooseChannel.dao.getUserChooseChannel(Integer.valueOf(ugcId));
            for(UserChooseChannel userChooseChannel :chooseChannels){
                Integer chooseChannel = userChooseChannel.getInt("choose_channel");
                if(chooseChannel==1){ //好评
                    weight = weight + 2;
                } else if(chooseChannel == 2){//中评
                    weight = weight + 1;
                } else if(chooseChannel == 3){//差评
                    weight = weight - 2;
                }
            }


            List<Comment> comments = Comment.comment.findCommentByUGCId(ugcId);
            for(Comment comment:comments){
                String userId = comment.getStr("user_id");//获取到用户的id

                //去检查用户选择
                UserChooseChannel userChooseChannel = UserChooseChannel.dao.getUserChooseChannel(Integer.valueOf(ugcId),userId);
                if(userChooseChannel!=null){
                    Integer chooseChannel = userChooseChannel.getInt("choose_channel");
                    if(chooseChannel==1){ //好评
                        weight = weight + 1.5;
                    } else if(chooseChannel == 2){//中评
                        weight = weight + 1;
                    } else if(chooseChannel == 3){//差评
                        weight = weight - 0.5;
                    } else {
                        weight = weight+1;
                    }
                } else { //如果投过票

                    weight = weight+1;
                }
                //设计
            }
        } else { //普通评论
            Comment comment = Comment.comment.findCommentSizeByUGC(ugcId);
            weight = comment.getLong("size");
        }
        //根据类型统计分数


        UgcHot ugcHot = UgcHot.dao.findByUgcId(Integer.valueOf(ugcId));

        if(ugcHot==null){
            ugcHot = new UgcHot();
            ugcHot.set("ugc_id",ugcId).set("hot_weight",weight).set("ugc_type",ugcType).set("prod_id", prodComment.getInt("prod_id")).save();
        } else {
            ugcHot.set("hot_weight",weight).update();
        }

    }
}
